package com.sailfish.springbootdemo.service.db1;

import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.configs.UserHeaderHolder;
import com.sailfish.springbootdemo.dao.db1.BrandDao;
import com.sailfish.springbootdemo.dao.db1.BrandTypeDao;
import com.sailfish.springbootdemo.dao.db1.PcbaDeviceResourceDao;
import com.sailfish.springbootdemo.dao.db1.PcbaDeviceTypeDao;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.Brand;
import com.sailfish.springbootdemo.pojo.db1.BrandType;
import com.sailfish.springbootdemo.pojo.db1.PcbaDeviceResource;
import com.sailfish.springbootdemo.pojo.db1.PcbaDeviceType;
import com.sailfish.springbootdemo.pojo.db4.Operate;
import com.sailfish.springbootdemo.service.db4.OperateService;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandDao brandDao;

    @Autowired
    private BrandTypeDao brandTypeDao;

    @Autowired
    private PcbaDeviceResourceDao pcbaDeviceResourceDao;

    @Autowired
    private CommonJudgeCanDeleteService commonService;

    @Autowired
    private PcbaDeviceTypeDao pcbaDeviceTypeDao;

    @Autowired
    private OperateService operateService;

    private String operateSmallType = "品牌/型号管理";

    @Override
    public List<Brand> getAllBrand(String searchValue) {
        List<Brand> list = brandDao.getAllBrand(searchValue);
        for (Brand brand : list) {
            brand.setBrandTypeList(brandTypeDao.getAllBrandType(null, brand.getBrandId()));
        }
        return list;
    }

    @Override
    public Brand getBrandById(Integer brandId) {
        return brandDao.getBrandById(brandId);
    }

    @Override
    public Result deleteBrandById(Integer brandId) {
        try {
            if (brandId == null || "".equals(brandId.toString())) {
                return ResultUtil.error(500, "缺少参数brandId", "Miss Param brandId", null);
            }

            String userId = UserHeaderHolder.getUserId();
            Brand oldBrand = brandDao.getBrandById(brandId);

            Map<String, Integer> stringIntegerMap = commonService.judgeCanDeleteBrand(brandId);
            if (stringIntegerMap.get("brandTypeNum") > 0 || stringIntegerMap.get("pcbaDeviceResourceNum") > 0) {
                return ResultUtil.error(500,
                        "禁止删除，当前项" + stringIntegerMap.get("brandTypeNum") + "处关联型号，" + stringIntegerMap.get("pcbaDeviceResourceNum") + "处关联设备资源池",
                        "Forbidden Delete", null);
            }
            Integer integer = brandDao.deleteBrandById(brandId);
            if (integer > 0) {
                // 操作记录
                operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 2, 1, new Date(), JSONObject.toJSONString(oldBrand), null));
                return ResultUtil.success(null);
            }
            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result addOrUpdateBrand(String mode, Brand brand) {
        try {
            if (brand == null) {
                return ResultUtil.error(500, "缺少参数brand", "Miss Param brand", null);
            }
            if (mode == null || "".equals(mode)) {
                return ResultUtil.error(500, "缺少参数mode", "Miss Param mode", null);
            }
            if (!("edit".equals(mode) || "add".equals(mode))) {
                return ResultUtil.error(500, "mode 参数错误", "mode Param Error", null);
            }

            String userId = UserHeaderHolder.getUserId();

            if ("edit".equals(mode)) {
                if (brand.getBrandId() == null || "".equals(brand.getBrandId().toString())) {
                    return ResultUtil.error(500, "缺少参数brandId", "Miss Param brandId", null);
                }

                List<Brand> brandList = brandDao.judgeUniqueBrand(brand.getBrandId(), brand.getBrandName());
                if (brandList.size() > 0) {
                    return ResultUtil.error(500, "修改失败，存在相同名字", "Operate Fail", null);
                }

                Brand oldBrand = brandDao.getBrandById(brand.getBrandId());

                brand.setUpdateTime(new Date());
                Integer integer = brandDao.updateBrand(brand);
                if (integer > 0) {
                    // 操作记录
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 3, 1, new Date(), JSONObject.toJSONString(oldBrand), JSONObject.toJSONString(brand)));
                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            } else {
                List<Brand> brandByBrandName = brandDao.getBrandByBrandName(brand.getBrandName());
                if (brandByBrandName.size() > 0) {
                    return ResultUtil.error(500, "该品牌已存在", "Already Exist", null);
                }
                brand.setCreateTime(new Date());
                Integer integer = brandDao.addBrand(brand);
                if (integer > 0) {
                    // 操作记录
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 1, 1, new Date(), null, JSONObject.toJSONString(brand)));
                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result getAllBrandByDeviceType(Integer deviceType, Integer isTechOwn, Integer status) {
        try {
            List<PcbaDeviceResource> allBrandByDeviceType = pcbaDeviceResourceDao.getAllBrandByDeviceType(deviceType, isTechOwn, status);
            List<Brand> brandList = new ArrayList<>();
            for (PcbaDeviceResource pcbaDeviceResource : allBrandByDeviceType) {
                if (pcbaDeviceResource != null && pcbaDeviceResource.getBrandId() != null && !"".equals(pcbaDeviceResource.getBrandId().toString())) {
                    Brand brand = brandDao.getBrandById(pcbaDeviceResource.getBrandId());
                    if (!brandList.contains(brand)) {
                        brandList.add(brand);
                    }
                }
            }
            for (Brand brand : brandList) {
                List<BrandType> brandTypeList = new ArrayList<>();
                for (PcbaDeviceResource pcbaDeviceResource : allBrandByDeviceType) {
                    BrandType brandType = brandTypeDao.getBrandTypeById(pcbaDeviceResource.getBrandTypeId());
                    if (brandType != null) {
                        BrandType brandTypeById = brandTypeDao.getBrandTypeByBrandAndTypeId(brandType.getBrandTypeId(), brand.getBrandId());
                        if (brandTypeById != null) {
                            if (!brandTypeList.contains(brandTypeById)) {
                                brandTypeList.add(brandTypeById);
                            }
                        }
                    }
                }
                brand.setBrandTypeList(brandTypeList);
            }
            return ResultUtil.success(brandList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result getAllBrandByDeviceTypes(String deviceTypes, Integer isTechOwn, Integer status) {
        try {
            if (deviceTypes == null || "".equals(deviceTypes)) {
                return ResultUtil.success(null);
            }
            List<JSONObject> allList = new ArrayList<>();
            String[] pcbaDeviceArr = deviceTypes.split(",");
            for (String deviceType : pcbaDeviceArr) {
                PcbaDeviceType pcbaDeviceTypeDaoById = pcbaDeviceTypeDao.getById(Integer.parseInt(deviceType));
                List<PcbaDeviceResource> allBrandByDeviceType = pcbaDeviceResourceDao.getAllBrandByDeviceType(Integer.parseInt(deviceType), isTechOwn, status);
                List<Brand> brandList = new ArrayList<>();
                for (PcbaDeviceResource pcbaDeviceResource : allBrandByDeviceType) {
                    if (pcbaDeviceResource != null && pcbaDeviceResource.getBrandId() != null && !"".equals(pcbaDeviceResource.getBrandId().toString())) {
                        Brand brand = brandDao.getBrandById(pcbaDeviceResource.getBrandId());
                        if (!brandList.contains(brand)) {
                            brandList.add(brand);
                        }
                    }
                }
                for (Brand brand : brandList) {
                    List<BrandType> brandTypeList = new ArrayList<>();
                    for (PcbaDeviceResource pcbaDeviceResource : allBrandByDeviceType) {
                        BrandType brandType = brandTypeDao.getBrandTypeById(pcbaDeviceResource.getBrandTypeId());
                        if (brandType != null) {
                            BrandType brandTypeById = brandTypeDao.getBrandTypeByBrandAndTypeId(brandType.getBrandTypeId(), brand.getBrandId());
                            if (brandTypeById != null) {
                                if (!brandTypeList.contains(brandTypeById)) {
                                    brandTypeList.add(brandTypeById);
                                }
                            }
                        }
                    }
                    brand.setBrandTypeList(brandTypeList);
                }
                JSONObject jo = new JSONObject();
                jo.put("pcbaDeviceType", pcbaDeviceTypeDaoById);
                jo.put("brandList", brandList);
                allList.add(jo);
            }
            return ResultUtil.success(allList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }
}
